package com.ikkong.shine.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.model.AjaxResult;
import com.ikkong.core.toolbox.Func;
import com.ikkong.core.toolbox.Record;
import com.ikkong.shine.meta.intercept.PagerIntercept;
import com.ikkong.shine.model.Pager;
import com.ikkong.shine.service.PagerService;
import com.ikkong.shine.service.impl.PagerServiceImpl;
import com.ikkong.shine.tools.Shared;
import com.ikkong.system.controller.base.UrlPermissController;
import com.jfinal.kit.JsonKit;



/**
 * Generated by Blade.
 * 2017-03-17 13:33:01
 */
public class PagerController extends UrlPermissController {
	private static String CODE = "pager";
	private static String PERFIX = "pager";
	private static String LIST_SOURCE = "Pager.list";
	private static String VIEW_SOURCE="Pager.view";
	private static String BASE_PATH = "/platform/pager/";
	Shared shared=new Shared();
	
	PagerService service = new PagerServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "pager.html");
	}

	public void add() {
		setAttr("code", CODE);
		List<Record> list_triageName= service.queryByTriageName();
		
		setAttr("list_triageName", list_triageName);
		render(BASE_PATH + "pager_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Pager pager = service.findById(id);
		setAttr("model", JsonKit.toJson(pager));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "pager_edit.html");
	}

	public void view() {
		String id = getPara(0);
		//Pager pager = service.findById(id);
		@SuppressWarnings("unchecked")
		Map<String, Object> map=Blade.dao().selectSingle(VIEW_SOURCE, Record.create().set("id", id), Map.class);
		if(null != map && null != map.get("type")){
			map.put("type", Func.getDictName(115, map.get("type")));
		}
		setAttr("model", JsonKit.toJson(map));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "pager_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new PagerIntercept());
		renderJson(grid);
	}

	public void save() {
		Pager pager = mapping(PERFIX, Pager.class);
		Pager pager_name=service.findByName(pager.getName());
		if(null!=pager_name&&pager_name.getId()!=null){
			renderJson(error("该叫号器名称:"+pager.getName()+"已存在！"));
			return;
		}
		Pager pager_ip=service.findByIP(pager.getIp());
		if(null!=pager_ip&&pager_ip.getId()!=null){
			renderJson(error("该IP："+pager.getIp()+"已存在！"));
			return;
		}else if(shared.isIP(pager.getIp())==false){
			renderJson(error("该IP:"+pager.getIp()+"不合法"));
			return;
		}else if(shared.isIpReachable(pager.getIp())==false){
			renderJson(error("该IP："+pager.getIp()+"无法ping通，请确认网络正常"));
			return;
		}
		/*String triage_id= getPara("pager.triage_id");
		if(null==triage_id||"".equals(triage_id)){
			renderJson(error("没有添加分诊台,不能添加叫号器操作"));
			return;
		}*/
		pager.setLogin_time(new Date());
		pager.setState(0);
		boolean temp = service.save(pager);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
		
	}

	public void update() {
		Pager pager = mapping(PERFIX, Pager.class);
		boolean temp = service.update(pager);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
	
	public void select_pager(){
		int type=getParaToInt("type",1);
		int aid=getParaToInt("aid",0);
		
		String sql="select id,name,display_name,ip from pager ";
		//已选择
		if(type==1){
			sql += " where  triage_id="+aid;
		}else{//未选择
			sql += " where triage_id IS NULL ";
		}
		
		Object grid = paginate(sql);
		
		renderJson(grid);
	}
	
	public void updateChild(){
		
		int type=getParaToInt("type",1);
		int aid=getParaToInt("aid");
		int gid=getParaToInt("gid");
		
		Record record=Record.create().set("aid", aid).set("gid", gid);
		AjaxResult result=new AjaxResult();
		
		result.setCode(0);
		if(type==1){
			
			String sql="UPDATE pager set triage_id=#{aid} where id =#{gid}";
			Db.init().update(sql,record);
			result.setMessage("添加成功");
		}else{
			String sql="UPDATE pager set triage_id=NULL where id =#{gid}";
			
			record.remove("aid");
			
			Db.init().update(sql, record);
			result.setMessage("移除成功");
		}
		renderJson(result);
		
	}
	
	public void pagerqueuetype(){
		setAttr("code", CODE);
		setAttr("aid", getPara(0));
		render(BASE_PATH+"pager_pagerqueuetype.html");
	}
}
